import axios from 'axios'
import Config from '../../renderer/src/config'

export async function useGetSystem(mainWindow) {
  axios.get(Config.baseApiUrl + '/api/system?id=100').then(({ data }) => {
    mainWindow.webContents.send('get-system-config', data.data)
  })
}

// 更新频道信息专用
export function useUpdateAccount(ipcMain) {
  ipcMain.on('update-account-info', (event, args) => {
    axios.put(Config.baseApiUrl + '/api/edit', args).then(({ data }) => {
      console.log(data, '-----')
    })
  })
}

export function useLogin(ipcMain) {
  // 监听登录，客户端发起会有问题
  ipcMain.on('login', async (event, args) => {
    // 为了监听是定时器还是其他事件，包装了一个额外参数标识
    const { external } = args
    const obj = {
      ...args
    }
    delete obj.external
    axios
      .get(Config.baseApiUrl + '/api/getkey', {
        params: obj
      })
      .then(({ data }) => {
        event.reply('login-result', {
          success: true,
          data: data,
          ...(external || {})
        })
      })
      .catch((err) => {
        event.reply('login-result', {
          success: false,
          data: err,
          ...(external || {})
        })
      })
  })
}
